using System;
using System.Collections.Generic;
using System.Text;
using Evolution.Engine.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Evolution.Engine.Managers;

namespace Evolution.Engine.Objects
{
    public class SkyDome : BaseObject, Evolution.Engine.Interfaces.IUpdateable, ILoadable, IChildRenderer
    {
        private Model _Model;
        private string _ModelAsset;
        private string _DomeTextureAsset;
        private Texture2D _DomeTexture;

        public SkyDome(string name, string model, string texture)
            : base(name)
        {
            _Rotation = new Quaternion(-MathHelper.ToRadians(45), 0, 0, 1);
            _Scale = new Vector3(750, 750, 750);
            _ModelAsset = model;
            _DomeTextureAsset = texture;
        }

        #region IUpdateable Members

        public override void Update(GameTime gameTime)
        {
            _Position = PlayerManager.Instance.Player.Position - new Vector3(0, 200, 0);
        }

        #endregion

        #region ILoadable Members

        public override void LoadContent()
        {
            if (_Model == null)
            {
                _Model = ResourceManager.Instance.LoadContent<Model>(_ModelAsset);

                foreach (ModelMesh mesh in _Model.Meshes)
                    foreach (BasicEffect currenteffect in mesh.Effects)
                        _DomeTexture = currenteffect.Texture;

                foreach (ModelMesh modmesh in _Model.Meshes)
                    foreach (ModelMeshPart modmeshpart in modmesh.MeshParts)
                        modmeshpart.Effect = ShaderManager.Instance.GetShader(_Shader).Effect.Clone(GameEngine.Instance.Device);
            }
        }

        #endregion

        #region IChildRenderer Members

        public void RenderChildren(GameTime gameTime)
        {
            Effect effect = ShaderManager.Instance.GetShader(_Shader).Effect;

            foreach (ModelMesh modmesh in _Model.Meshes)
            {
                foreach (Effect currenteffect in modmesh.Effects)
                {
                    currenteffect.CurrentTechnique = effect.Techniques["Textured"];

                    currenteffect.Parameters["World"].SetValue(World);
                    currenteffect.Parameters["View"].SetValue(CameraManager.Instance.ActiveCamera.View);
                    currenteffect.Parameters["Projection"].SetValue(CameraManager.Instance.ActiveCamera.Projection);
                    currenteffect.Parameters["xTexture"].SetValue(_DomeTexture);
                }

                modmesh.Draw();
            }
        }

        #endregion
    }
}
